/* mp_utils.c --- Funções de auxílio
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogofsr@grad.icmc.usp.br>
 *                     Bruno Ribeiro de Oliveira
 *                     Arineiza Cristina Pinheiro
 *                     Tomas Grover
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stdio.h>
#include "mp_utils.h"

static void error_message( int exit_status,
			   const char *mode,
			   const char *message );

void
error_message( int exit_status, const char *mode, const char *message )
{
    fprintf( stderr, "marco-polo:%s:%s\n", mode, message );

    if( exit_status >= 0 )
    {
	exit(exit_status);
    }

    return;
}

void
xerror_warning( const char *message )
{
    error_message( -1, "warning", message );

    return;
}

void
xerror_error( const char *message )
{
    error_message( -1, "error", message );

    return;
}

void
xerror_fatal( const char *message )
{
    error_message( 0, "FATAL", message );

    return;
}

void *
xmalloc( size_t num )
{
    void *new = malloc( num );

    if( !new )
	xerror_fatal("No memory, AT ALL!!!");

    return new;
}

void *
xrealloc( void *ptr, size_t num )
{
    void *new = NULL;

    if( !ptr )
	return xmalloc( num );

    new = realloc( ptr, num );
    if( !new )
	xerror_fatal("No memory, AT ALL!!!");

    return new;
}

void
xfree( void *ptr )
{
    if( ptr )
    {
	free(ptr);
	ptr = 0;
    }

    return;
}
